package com.streetpower.common.result;

import java.io.Serializable;

public class Response <T> implements Serializable{
	private static final long serialVersionUID = 1L;
	private int code;
	private String message;
	private Boolean success;
	private T data;

	public Response(Boolean success , int code){
		this.success = success;
		this.code = code;
	}

	public Response(){

	}

	/**通用成功
	 * @param data
	 * @return
	 */
	public static <T> Response<T> success(T data){
		Response<T> response = new Response<T>();
		response.data = data;
		response.code = StatusCode.SUCCESS.getStatus();
		response.message = StatusCode.SUCCESS.getMsg();
		response.success=true;
		return response;
	}

	/**通用重定向
	 * @param data
	 * @return
	 */
	public static <T> Response<T> redirect(T data){
		Response<T> response = new Response<T>();
		response.data = data;
		response.code = StatusCode.REDIRECT.getStatus();
		response.message = StatusCode.REDIRECT.getMsg();
		return response;
	}

	/**
	 * 通用未知异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> unKnowError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.UNKNOW_ERROR.getStatus();
		response.message = message == null?StatusCode.UNKNOW_ERROR.getMsg():message;
		return response;
	}

	/**
	 * 通用业务异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> bussinessError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.BUSSINESS_ERROR.getStatus();
		response.message = message == null?StatusCode.BUSSINESS_ERROR.getMsg():message;
		return response;
	}

	/**
	 * 通用系统异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> systemError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.SYSTEM_ERROR.getStatus();
		response.message = message == null?StatusCode.SYSTEM_ERROR.getMsg():message;
		return response;
	}

	/**
	 * 通用数据库异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> databaseError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.DATABASE_ERROR.getStatus();
		response.message = message == null?StatusCode.DATABASE_ERROR.getMsg():message;
		return response;
	}

	/**
	 * 通用参数不正确异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> paramError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.PARAM_ERROR.getStatus();
		response.message = message == null?StatusCode.PARAM_ERROR.getMsg():message;
		return response;
	}


	/**
	 * 通用解密异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> encryptError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.ENCRYPT_ERROR.getStatus();
		response.message = message == null?StatusCode.ENCRYPT_ERROR.getMsg():message;
		return response;
	}

	/**
	 * 通用缓存异常
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> cacheError(String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = StatusCode.CACHE_ERROR.getStatus();
		response.message = message == null?StatusCode.CACHE_ERROR.getMsg():message;
		return response;
	}


	/**
	 * 通用失败异常
	 * @param code
	 * @param message
	 * @param <T>
	 * @return
	 */
	public static <T> Response<T> error(int code,String message){
		Response<T> response = new Response<T>();
		response.data = null;
		response.code = code;
		response.success=false;
		response.message = message;
		return response;
	}


	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
}
